package com.ibix.tools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/2/5.
 */
@Component
public class RedisTool {

    @Autowired
    private StringRedisTemplate template;

    public static RedisTool redisTool;

    public static RedisTool getRedisTool() {
        return redisTool;
    }

    public static void setRedisTool(RedisTool redisTool) {
        RedisTool.redisTool = redisTool;
    }

    public void setKey(String key, String value)
    {
        ValueOperations<String,String> operations = template.opsForValue();
        operations.set(key, value);
    }

    public void setKey(String key,String value,long time)
    {
        ValueOperations<String,String> operations = template.opsForValue();
        operations.set(key, value, time, TimeUnit.SECONDS);
    }

    public String getValue(String key)
    {
        ValueOperations<String,String> operations = template.opsForValue();
        return operations.get(key);
    }

    public void deleteKey(String key)
    {
        template.delete(key);
    }
}
